package com.nononsenseapps.feeder.db.room

import androidx.room.Dao
import androidx.room.Query
import cn.ekuma.odbo.mobile.room.BaseDao
import com.nononsenseapps.feeder.db.FEED_ITEM_PRELOAD_ITEM
import com.nononsenseapps.feeder.db.entity.FeedItemPreLoadItem
import com.nononsenseapps.feeder.db.entity.PROCESS_STATE_INIT
import com.nononsenseapps.feeder.db.entity.PROCESS_STATE_PROCESSED


@Dao
interface FeedItemPreLoadItemDao: BaseDao<FeedItemPreLoadItem> {

    @Query("SELECT * FROM ${FEED_ITEM_PRELOAD_ITEM.tableName} WHERE feed_item_id = :itemId and ${FEED_ITEM_PRELOAD_ITEM.COL_IS_ENCLOSURE} = :isEnclosure")
    fun load(itemId: Long,isEnclosure:Boolean=true): FeedItemPreLoadItem

    @Query("SELECT * FROM ${FEED_ITEM_PRELOAD_ITEM.tableName} WHERE id = :id ")
    fun get(id: Long): FeedItemPreLoadItem?

    @Query("DELETE FROM ${FEED_ITEM_PRELOAD_ITEM.tableName} WHERE feed_item_id = :itemId and ${FEED_ITEM_PRELOAD_ITEM.COL_IS_ENCLOSURE} = :isEnclosure")
    fun delete(itemId: Long,isEnclosure:Boolean=true)

    @Query("SELECT * FROM ${FEED_ITEM_PRELOAD_ITEM.tableName} WHERE ${FEED_ITEM_PRELOAD_ITEM.COL_RRELOAD_URL} = :sourceLink and ${FEED_ITEM_PRELOAD_ITEM.COL_LOAD_STATE} = $PROCESS_STATE_PROCESSED")
    fun findBySourceDownLoaded(sourceLink: String): FeedItemPreLoadItem?

    @Query("SELECT * FROM ${FEED_ITEM_PRELOAD_ITEM.tableName} WHERE feed_item_id = :itemID and is_enclosure = :isEnclosure")
    fun findEnclosureLoadItem(itemID: Long, isEnclosure:Boolean=true): FeedItemPreLoadItem?

    @Query("UPDATE ${FEED_ITEM_PRELOAD_ITEM.tableName} SET ${FEED_ITEM_PRELOAD_ITEM.COL_LOAD_STATE} = :state WHERE ${FEED_ITEM_PRELOAD_ITEM.COL_RRELOAD_URL} = :preLoadUrl")
    fun updateFeedItemPreLoadState(preLoadUrl: String, state: Int)

    @Query("UPDATE ${FEED_ITEM_PRELOAD_ITEM.tableName} SET ${FEED_ITEM_PRELOAD_ITEM.COL_LOAD_STATE} = :state ,${FEED_ITEM_PRELOAD_ITEM.COL_LOCAL_URI} = :localUri,${FEED_ITEM_PRELOAD_ITEM.COL_TYPE} =:mediaType ,${FEED_ITEM_PRELOAD_ITEM.COL_SIZE} = :totalBytes WHERE ${FEED_ITEM_PRELOAD_ITEM.COL_RRELOAD_URL} = :preLoadUrl")
    fun completeFeedItemPreLoad(preLoadUrl: String, localUri: String,mediaType:String,totalBytes:Int, state: Int = PROCESS_STATE_PROCESSED)

    @Query("SELECT count(*) FROM ${FEED_ITEM_PRELOAD_ITEM.tableName} WHERE ${FEED_ITEM_PRELOAD_ITEM.COL_RRELOAD_URL} = :preLoadUrl")
    fun findPreDownLoadItemCount(preLoadUrl: String): Int

    @Query("SELECT id FROM ${FEED_ITEM_PRELOAD_ITEM.tableName} WHERE ${FEED_ITEM_PRELOAD_ITEM.COL_LOAD_STATE} in ($PROCESS_STATE_INIT)")
    fun loadMustPreLoaderItem(): List<Long>

    @Query("DELETE FROM ${FEED_ITEM_PRELOAD_ITEM.tableName} WHERE ${FEED_ITEM_PRELOAD_ITEM.COL_RRELOAD_URL} = :preLoadUrl")
    fun removeDownLoadFile(preLoadUrl: String)

}

fun FeedItemPreLoadItemDao.upsert(item: FeedItemPreLoadItem): Long = when (item.id > ID_UNSET) {
    true -> {
        update(item)
        item.id
    }
    false -> {
        val id=insert(item)
        item.id=id
        id
    }
}